package github.hellocsl.gallerylayoutmanager;

import github.hellocsl.gallerylayoutmanager.layout.impl.ScaleTransformer;
import github.hellocsl.gallerylayoutmanager.provider.TestBottomProvider;
import github.hellocsl.gallerylayoutmanager.provider.TestTopProvider;
import github.hellocsl.gallerylayoutmanager.util.ToastUtil;
import github.hellocsl.layoutmanager.gallery.GalleryListContainer;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class TestFraction extends Fraction {
    private GalleryListContainer lcTopList;
    private final List<String> listTopData = new ArrayList<>();
    private GalleryListContainer lcBottomList;
    private final List<String> listBottomData = new ArrayList<>();
    private boolean isFirst = true;
    private StringBuffer topHint;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component mComponent = scatter.parse(ResourceTable.Layout_fraction_test, container, false);
        init(mComponent);
        return mComponent;
    }

    /**
     * 初始化
     *
     * @param mComponent 布局组件
     */
    private void init(Component mComponent) {
        isFirst = true;
        topHint = new StringBuffer();
        lcTopList = (GalleryListContainer) mComponent.findComponentById(ResourceTable.Id_lc_test_top_list);
        lcTopList.setItemTransformer(new ScaleTransformer());
        lcBottomList = (GalleryListContainer) mComponent.findComponentById(ResourceTable.Id_lc_test_bottom_list);
        Text textTopHint = (Text) mComponent.findComponentById(ResourceTable.Id_text_test_top_hint);
        Text textBottomHint = (Text) mComponent.findComponentById(ResourceTable.Id_text_test_bottom_hint);
        textTopHint.setText("");
        textBottomHint.setText("");
        Button btnRandom = (Button) mComponent.findComponentById(ResourceTable.Id_btn_test_random_select);
        Button btnChange = (Button) mComponent.findComponentById(ResourceTable.Id_btn_test_data_change);

        btnRandom.setClickedListener(component -> {
            int position = new SecureRandom().nextInt(listTopData.size());
            lcTopList.scrollToCenter(position);
            lcBottomList.scrollToCenter(position);
        });

        listTopData.clear();
        listBottomData.clear();
        for (int i = 0; i < 50; i++) {
            listTopData.add("Hello"+i);
            listBottomData.add("Hello"+i);
        }
        TestTopProvider providerTop = new TestTopProvider(getFractionAbility(), listTopData);
        lcTopList.setOrientation(Component.HORIZONTAL);
        lcTopList.setItemProvider(providerTop);
        lcTopList.setItemClickedListener((listContainer, component, position, l) -> {
            lcTopList.clickedScrollByCenter(position);
        });
        providerTop.setOnCreateComponentHolderListener(() -> {
            topHint.append("Create VH type:+0\n");
            if (!isFirst) {
                textTopHint.setText(topHint.toString());
            }
        });

        TestBottomProvider providerBottom = new TestBottomProvider(getFractionAbility(), listBottomData);
        lcBottomList.setOrientation(Component.VERTICAL);
        lcBottomList.setItemProvider(providerBottom);

        lcBottomList.setItemClickedListener((listContainer, component, position, l) -> {
            lcBottomList.setAnimatorDuration(listBottomData.size());
            lcBottomList.clickedScrollByCenter(position);
        });
        lcBottomList.setItemSelectedCallback((item, position) -> {
            EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
            eventHandler.postTask(() -> {
                if (isFirst) {
                    eventHandler.postTask(() -> {
                        isFirst = false;
                    }, 100);
                    textBottomHint.setText("");
                }else{
                    textBottomHint.setText("selected:" + position);
                }
            }, 5);
        });
        lcBottomList.scrollToCenter(15);

        btnChange.setClickedListener(component -> {
            if (new SecureRandom().nextBoolean()) {
                for (int i = 0; i < 10; i++) {
                    listTopData.add("Extra:" + i);
                    listBottomData.add("Extra:" + i);
                }
                ToastUtil.getInstance().showToast(getFractionAbility(),"add data");
            } else {
                int size = listTopData.size();
                int cut = size / 2;
                for (int i = size - 1; i > cut; i--) {
                    listTopData.remove(i);
                    listBottomData.remove(i);
                }
                ToastUtil.getInstance().showToast(getFractionAbility(),"remove data");
            }
            providerTop.notifyDataChanged();
            providerBottom.notifyDataChanged();
        });
    }
}
